<?php
namespace Api\Controller;
use Think\Controller;
class ArticleController extends BaseController{
  /**
  *获取文章分类 get
  *@param: 
  *@return:
  */
  public function getCategory(){
    $data = M('category')
    ->field('id,title')
    ->where(array('display'=>1,'status'=>1))
    ->select();
    $this->response(array('info'=>$data),json,200);
  }

  /**
  *获取文章分类列表 get
  *@param: listId
  *@return:
  */
  public function getCategoryList(){
    $listId = I('get.listId');
    if(!$listId){
      $this->response(array('info'=>'请传递listId'),json,400);
    }
    $data = M('category')
    ->field('category.id,title,icon,picture.path as cover')
    ->where(array('display'=>1,'category.status'=>1,'pid'=>$listId))
    ->join('picture on picture.id = category.cover_id','left')
    ->order('sort asc')
    ->select();
    foreach ($data as $key => $value) {
      # code...
      $data[$key]['children'] = M('category')
      ->field('category.id,title,icon')
      ->where(array('display'=>1,'category.status'=>1,'pid'=>$value['id']))
      ->order('sort asc')
      ->select();

      $data[$key]['articles'] = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$value['id'],'display'=>1,'document.status'=>1))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->page(1,5)
      ->select();
    }
    $this->response(array('info'=>$data),json,200);
  }

  /**
  *获取文章列表 get
  *@param: category_id,page,type(1:图文2:视频)
  *@return:
  */
  public function getArticles(){
    $category_id = I('get.category_id');
    $page = I('get.page')?I('get.page'):1;
    if(!$category_id){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    $type = I('get.content_type');
    if($type){
      $data = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1,'content_type'=>$type))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->page($page,5)
      ->select();

      $total = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1,'content_type'=>$type))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->select();
    }else{
      $data = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->page($page,5)
      ->select();

      $total = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->select();
    }
    $this->response(array('info'=>$data,'total'=>count($total)),json,200);
  }

  /**
  *搜索文章 get
  *@param: category_id,keywords
  *@return:
  */
  public function searchArticles(){
    $category_id = I('get.category_id');
    $page = I('get.page')?I('get.page'):1;
    $keywords = I('get.keywords');
    if(!$category_id){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    $map['title'] = array('like','%'.$keywords.'%');
    if($keywords){
      $data = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1))
      ->where($map)
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->page($page,5)
      ->select();

      $total = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1))
      ->where($map)
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->select();
    }else{
      $data = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->page($page,5)
      ->select();

      $total = M('document')
      ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.bookmark, picture.path,writer,content_type')
      ->where(array('category_id'=>$category_id,'display'=>1,'document.status'=>1))
      ->join('picture on picture.id = document.cover_id','left')
      ->join('document_article on document_article.id = document.id')
      ->order('level desc,create_time desc')
      ->select();
    }
    
    $this->response(array('info'=>$data,'total'=>count($total)),json,200);
  }

  /**
  *获取文章
  *@param: article_id:文章id
  *@return:
  */
  public function getArticleDetail(){
    $article_id = I('get.article_id');
    if(!$article_id){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    $data = M('document')
    ->where(array('document.id'=>$article_id))
    ->field('document.id,title,category_id,description,display,view,document.create_time,document_article.content,document_article.bookmark,writer')
    ->join('document_article on document_article.id = document.id')
    ->find();
    $this->response(array('info'=>$data),json,200);
  }

  /**
  *增加阅读量 post
  *@param: article_id
  *@return:
  */
  public function addView(){
    $post = json_decode(file_get_contents('php://input'), true);
    // $post = I('post.');
    if(!$post['article_id']){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    $data = M('document')
    ->where(array('document.id'=>$post['article_id']))
    ->setInc('view');

    $new_data = M('document')->find($post['article_id']);
    $this->response(array('info'=>$new_data),json,200);
  }

  /**
  *获取faq问题列表，不分页 get
  *@param: faqId
  *@return:
  */
  public function getFaqs(){
    $faqId = I('get.faqId');
    $page = I('get.page')?I('get.page'):1;
    if(!$faqId){
      $this->response(array('info'=>'参数错误'),json,400);
    }
    $data = M('document')
    ->field('document.id,title,category_id,description,display,document.status,document.create_time,document_article.content')
    ->where(array('category_id'=>$faqId,'display'=>1,'document.status'=>1))
    ->join('document_article on document_article.id = document.id')
    ->order('level desc,create_time desc')
    ->select();

    $faq = M('category')
    ->field('id,title,icon')
    ->where(array('id'=>$faqId))
    ->find();

    $this->response(array('info'=>$data,'faq'=>$faq),json,200);
  }

}